<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.7.2/css/all.min.css" integrity="sha256-dABdfBfUoC8vJUBOwGVdm8L9qlMWaHTIfXt+7GnZCIo=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"blog.csgrandeur.cn","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.22.0","exturl":false,"sidebar":{"position":"left","width_expanded":320,"width_dual_column":240,"display":"post","padding":18,"offset":12},"hljswrap":true,"copycode":{"enable":true,"style":"default"},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"duration":200,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"top_n_per_article":1,"unescape":false,"preload":false,"trigger":"auto"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="分块 字面意思，把数据分成一块一块去处理。 比如数据存在一段一段连续的相同值时，把相同的值看作一个的&quot;块&quot;，从而快速跳过。 比如把要经常批量处理的数据分成一个一个的块，在批量处理时，整个被覆盖的块做整体处理，没整个覆盖的再挨个处理。">
<meta property="og:type" content="article">
<meta property="og:title" content="24.分块">
<meta property="og:url" content="http://blog.csgrandeur.cn/2025-04-05-24-%E5%88%86%E5%9D%97/index.html">
<meta property="og:site_name" content="CSGrandeur&#39;s Thinking">
<meta property="og:description" content="分块 字面意思，把数据分成一块一块去处理。 比如数据存在一段一段连续的相同值时，把相同的值看作一个的&quot;块&quot;，从而快速跳过。 比如把要经常批量处理的数据分成一个一个的块，在批量处理时，整个被覆盖的块做整体处理，没整个覆盖的再挨个处理。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2025-04-05T12:42:34.000Z">
<meta property="article:modified_time" content="2025-04-10T10:55:09.436Z">
<meta property="article:author" content="CSGrandeur">
<meta property="article:tag" content="ACM">
<meta property="article:tag" content="Algorithm">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://blog.csgrandeur.cn/2025-04-05-24-%E5%88%86%E5%9D%97/">


<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://blog.csgrandeur.cn/2025-04-05-24-%E5%88%86%E5%9D%97/","path":"2025-04-05-24-分块/","title":"24.分块"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>24.分块 | CSGrandeur's Thinking</title>
  

  <script src="/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?7958adf931092425a489778560129144"></script>







  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">CSGrandeur's Thinking</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Cogito Ergo Sum</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
      <div class="search-header">
        <span class="search-icon">
          <i class="fa fa-search"></i>
        </span>
        <div class="search-input-container">
          <input autocomplete="off" autocapitalize="off" maxlength="80"
                placeholder="搜索..." spellcheck="false"
                type="search" class="search-input">
        </div>
        <span class="popup-btn-close" role="button">
          <i class="fa fa-times-circle"></i>
        </span>
      </div>
      <div class="search-result-container">
        <div class="search-result-icon">
          <i class="fa fa-spinner fa-pulse fa-5x"></i>
        </div>
      </div>
    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%88%86%E5%9D%97"><span class="nav-number">1.</span> <span class="nav-text">分块</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BE%8B%E7%AB%8B%E6%96%B9%E6%A0%B9"><span class="nav-number">1.1.</span> <span class="nav-text">例：立方根</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BE%8B%E5%BC%80%E5%85%B3"><span class="nav-number">1.2.</span> <span class="nav-text">例：开关</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%93%E5%85%A5"><span class="nav-number">1.2.1.</span> <span class="nav-text">输入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%93%E5%87%BA"><span class="nav-number">1.2.2.</span> <span class="nav-text">输出</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%90%AF%E5%8F%91"><span class="nav-number">1.3.</span> <span class="nav-text">启发</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">CSGrandeur</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">72</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">22</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://blog.csgrandeur.cn/2025-04-05-24-%E5%88%86%E5%9D%97/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="CSGrandeur">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="24.分块 | CSGrandeur's Thinking">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          24.分块
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2025-04-05 20:42:34" itemprop="dateCreated datePublished" datetime="2025-04-05T20:42:34+08:00">2025-04-05</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2025-04-10 18:55:09" itemprop="dateModified" datetime="2025-04-10T18:55:09+08:00">2025-04-10</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/" itemprop="url" rel="index"><span itemprop="name">ACM</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/ACM/ACMCOURSE/" itemprop="url" rel="index"><span itemprop="name">ACMCOURSE</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><h1 id="分块">分块</h1>
<p>字面意思，把数据分成一块一块去处理。</p>
<p>比如数据存在一段一段连续的相同值时，把相同的值看作一个的"块"，从而快速跳过。</p>
<p>比如把要经常批量处理的数据分成一个一个的块，在批量处理时，整个被覆盖的块做整体处理，没整个覆盖的再挨个处理。</p>
<span id="more"></span>
<p>分块首先是一种思想，其次可以用一些套路代码。</p>
<p>作为一种思想，一些问题可以不显式的去做某种代码结构，而是灵活地将数据分块处理即可。</p>
<p>还有一些时候，可以将数据模式化地分块，然后处理一些区间操作与查询。</p>
<h2 id="例立方根">例：立方根</h2>
<p>给定 <span class="math inline">\(q\)</span>
个询问，每个询问给出一个正整数 <span
class="math inline">\(x\)</span>，求所有不大于<span
class="math inline">\(x\)</span>的数的立方根向下取整之和：</p>
<p><span class="math inline">\(\sum_{j=1}^{x} \lfloor \sqrt[3]{j}
\rfloor\)</span></p>
<p>其中 <span class="math inline">\(\lfloor x \rfloor\)</span> 表示对
<span class="math inline">\(x\)</span> 向下取整。</p>
<p>题目按从小到大的顺序给出这些 <span
class="math inline">\(x\)</span></p>
<p>分析：<span class="math inline">\(x\)</span>范围很大，挨个枚举不大于
<span class="math inline">\(x\)</span>
的数必超时。但"立方根向下取整"的值其实是一段一段相同的值组成的，比如<span
class="math inline">\(1\sim 10\)</span> 的立方根是 <span
class="math inline">\(1,1,1,1,1,1,1,2,2,2\)</span>，把立方根相同的一段段数看作一个又一个的"块"，块内相同的值乘以块的长度，就能大跨步计算立方根向下取整之和。</p>
<p>这道题又按从小到大顺序给 <span
class="math inline">\(x\)</span>，那么也不用开数组存前缀结果了，保存之前计算过的最后一块即可。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="function"><span class="type">long</span> <span class="type">long</span> <span class="title">cube</span><span class="params">(<span class="type">long</span> <span class="type">long</span> x)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> x * x * x;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="type">long</span> <span class="type">long</span> q, x, last_3 = <span class="number">0</span>, last_3_sum = <span class="number">0</span>;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%lld&quot;</span>, &amp;q);</span><br><span class="line">    <span class="keyword">while</span>(q--) &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%lld&quot;</span>, &amp;x);</span><br><span class="line">        <span class="keyword">for</span>(;<span class="built_in">cube</span>(last_3 + <span class="number">1</span>) &lt;= x; last_3 ++) &#123;</span><br><span class="line">            last_3_sum += (<span class="built_in">cube</span>(last_3 + <span class="number">1</span>) - <span class="built_in">cube</span>(last_3)) * last_3;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;%lld\n&quot;</span>, last_3_sum + (x - <span class="built_in">cube</span>(last_3) + <span class="number">1</span>) * last_3);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="例开关">例：开关</h2>
<p>有 <span class="math inline">\(n\)</span> 盏灯，初始状态均为关闭。有
<span class="math inline">\(m\)</span>
次操作，每次操作为以下两种之一：</p>
<ol type="1">
<li>区间翻转：将区间 <span class="math inline">\([a,b]\)</span>
内的灯状态全部翻转</li>
<li>区间查询：统计区间 <span class="math inline">\([a,b]\)</span>
内开启的灯数量</li>
</ol>
<h3 id="输入">输入</h3>
<p>第一行：两个整数 <span class="math inline">\(n,m\)</span>
(灯的数量和操作次数)</p>
<p>接下来 <span class="math inline">\(m\)</span> 行：每行三个整数 <span
class="math inline">\(c,a,b\)</span></p>
<ul>
<li><span class="math inline">\(c=0\)</span>：区间翻转操作</li>
<li><span class="math inline">\(c=1\)</span>：区间查询操作</li>
<li><span class="math inline">\([a,b]\)</span>：操作区间范围</li>
</ul>
<figure class="highlight txt"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">4 5</span><br><span class="line">0 1 2</span><br><span class="line">0 2 4</span><br><span class="line">1 2 3</span><br><span class="line">0 2 4</span><br><span class="line">1 1 4</span><br></pre></td></tr></table></figure>
<h3 id="输出">输出</h3>
<p>对每个查询操作输出一行，表示区间内开启的灯数量</p>
<figure class="highlight txt"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td></tr></table></figure>
<p>显然一个一个去操作会很慢，但不同的区间会重叠，整体操作又难处理多次重叠的地方。</p>
<p>分块是对挨个操作和整体操作的折衷：将数据分成特定大小的一块一块，标记好块对应的区域。</p>
<p>更新操作：</p>
<ul>
<li><span class="math inline">\([a,b]\)</span>
区间完整覆盖某一块时，给这一块打个标记，表示整块都异或过一次，这一块的和应该变为块大小减去上一次的和</li>
<li><span class="math inline">\([a,b]\)</span>
区间没有完整覆盖某一块，但有交集，暴力更新这一块的每一个元素，但记得更新之前先应用曾经对整块打过的标记</li>
</ul>
<p>查找操作：</p>
<ul>
<li><span class="math inline">\([a,b]\)</span>
区间完整覆盖某一块时，直接累加这一块的和</li>
<li><span class="math inline">\([a,b]\)</span>
区间没有完整覆盖某一块，但有交集，暴力查询这一块的每个元素，也记得先应用曾经对整块打过的标记</li>
</ul>
<p>假设分块的大小是 <span class="math inline">\(\sqrt{n}\)</span>
，那么块的个数也是 <span
class="math inline">\(\sqrt{n}\)</span>。对于每个 <span
class="math inline">\([a,b]\)</span>，遍历所有块是 <span
class="math inline">\(\sqrt{n}\)</span>，至多首尾两个地方是部分覆盖，暴力处理
<span class="math inline">\(2\)</span> 个块，每个块内部 <span
class="math inline">\(\sqrt{n}\)</span> 个元素，所以处理每个 <span
class="math inline">\([a,b]\)</span> 的复杂度从 <span
class="math inline">\(O(n)\)</span>（最坏情况）降低到 <span
class="math inline">\(O(\sqrt{n})\)</span>。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstdio&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> maxn = <span class="number">2e5</span> + <span class="number">10</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> block_size = <span class="built_in">sqrt</span>(maxn);</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Block</span> &#123;</span><br><span class="line">    <span class="type">int</span> l;              <span class="comment">// 块的左起点</span></span><br><span class="line">    <span class="type">int</span> tag;            <span class="comment">// 对整块的操作标记（避免挨个处理）</span></span><br><span class="line">    <span class="type">int</span> sum;            <span class="comment">// 块内1的个数</span></span><br><span class="line">&#125;;</span><br><span class="line">Block bl[block_size + <span class="number">10</span>];</span><br><span class="line"><span class="type">int</span> n, m, c, a, b, bn;</span><br><span class="line"><span class="type">bool</span> sta[maxn];</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">Update</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; bn; i ++) &#123;</span><br><span class="line">        <span class="type">int</span> r = bl[i].l + block_size - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span>(bl[i].l &gt;= a &amp;&amp; r &lt;= b) &#123;</span><br><span class="line">            <span class="comment">// 整块都在覆盖内</span></span><br><span class="line">            bl[i].tag ^= <span class="number">1</span>;</span><br><span class="line">            bl[i].sum = block_size - bl[i].sum;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(bl[i].l &lt;= a &amp;&amp; a &lt;= r || bl[i].l &lt;= b &amp;&amp; b &lt;= r) &#123;</span><br><span class="line">            <span class="comment">// 部分覆盖，挨个处理</span></span><br><span class="line">            bl[i].sum = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> j = <span class="number">0</span>; j &lt; block_size; j ++) &#123;</span><br><span class="line">                sta[bl[i].l + j] ^= bl[i].tag;   <span class="comment">// 先应用之前记录的 tag</span></span><br><span class="line">                <span class="keyword">if</span>(bl[i].l + j &gt;= a &amp;&amp; bl[i].l + j &lt;= b) &#123;</span><br><span class="line">                    sta[bl[i].l + j] ^= <span class="number">1</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                bl[i].sum += sta[bl[i].l + j];   <span class="comment">// 重新统计块内和</span></span><br><span class="line">            &#125;</span><br><span class="line">            bl[i].tag = <span class="number">0</span>;  <span class="comment">// 每个元素都已最新，tag清空</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">Query</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span> </span>&#123;</span><br><span class="line">    <span class="type">int</span> ans = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; bn; i ++) &#123;</span><br><span class="line">        <span class="type">int</span> r = bl[i].l + block_size - <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span>(bl[i].l &gt;= a &amp;&amp; r &lt;= b) &#123;</span><br><span class="line">            ans += bl[i].sum;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span>(bl[i].l &lt;= a &amp;&amp; a &lt;= r || bl[i].l &lt;= b &amp;&amp; b &lt;= r) &#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> j = <span class="number">0</span>; j &lt; block_size; j ++) &#123;</span><br><span class="line">                sta[bl[i].l + j] ^= bl[i].tag;  <span class="comment">// 先应用之前记录的 tag，易忘，重视！！</span></span><br><span class="line">                ans += bl[i].l + j &gt;= a &amp;&amp; bl[i].l + j &lt;= b ? sta[bl[i].l + j] : <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            bl[i].tag = <span class="number">0</span>;   <span class="comment">// 每个元素都已最新，tag清空，易忘，重视！！</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ans;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>, &amp;n, &amp;m);</span><br><span class="line">    <span class="built_in">memset</span>(bl, <span class="number">0</span>, <span class="built_in">sizeof</span>(bl));</span><br><span class="line">    <span class="built_in">memset</span>(sta, <span class="number">0</span>, <span class="built_in">sizeof</span>(sta));</span><br><span class="line">    bn = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">1</span>; i &lt;= n; i += block_size) &#123;</span><br><span class="line">        bl[bn ++].l = i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span>(m --) &#123;</span><br><span class="line">        <span class="built_in">scanf</span>(<span class="string">&quot;%d%d%d&quot;</span>, &amp;c, &amp;a, &amp;b);</span><br><span class="line">        <span class="keyword">if</span>(c == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="built_in">Update</span>(a, b);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d\n&quot;</span>, <span class="built_in">Query</span>(a, b));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="启发">启发</h2>
<p>不要把分块当作处理区间问题的套路，它更是一种数据处理思想。毕竟区间数据处理有更高级的上位算法线段树，在那之后，处理这类区间问题时，并不会经常用分块。</p>
<p>掌握分块思想，应注重于在面对问题时，善于发现数据潜在特点，分块提升效率。</p>
<blockquote>
<p>未来前瞻：数论分块</p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/ACM/" rel="tag"># ACM</a>
              <a href="/tags/Algorithm/" rel="tag"># Algorithm</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2025-04-05-23-%E5%8D%95%E8%B0%83%E9%98%9F%E5%88%97/" rel="prev" title="23.单调队列">
                  <i class="fa fa-angle-left"></i> 23.单调队列
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2025-04-05-25-%E5%80%8D%E5%A2%9E%E4%B8%8E%E7%A8%80%E7%96%8F%E8%A1%A8/" rel="next" title="25.倍增与稀疏表">
                  25.倍增与稀疏表 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments utterances-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">CSGrandeur</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://fastly.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://fastly.jsdelivr.net/npm/@next-theme/pjax@0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/sidebar.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://fastly.jsdelivr.net/npm/hexo-generator-searchdb@1.4.1/dist/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>







  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://fastly.jsdelivr.net/npm/mathjax@3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


<script class="next-config" data-name="utterances" type="application/json">{"enable":true,"repo":"CSGrandeur/csgrandeur.github.io","issue_term":"pathname","theme":"github-light"}</script>
<script src="/js/third-party/comments/utterances.js"></script>

</body>
</html>
